package com.sugon.cloudgateway.service.impl;

import com.sugon.cloudgateway.service.RouteService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.event.RefreshRoutesEvent;
import org.springframework.cloud.gateway.route.RouteDefinition;
import org.springframework.cloud.gateway.route.RouteDefinitionWriter;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Mono;


@Service
@Slf4j
public class RouteServiceImpl implements RouteService, ApplicationEventPublisherAware {
    //private static final Logger LOGGER = LoggerFactory.getLogger(RouteServiceImpl.class);
    // RouteDefinitionWriter 提供了对路由的增加删除等操作
    @Autowired
    private RouteDefinitionWriter routeDefinitionWriter;

    /**
     * 事件发布者
     * 是ApplicationContext的父接口之一
     * 他的功能就是发布事件，也就是把某个事件告诉所有与这个事件相关的监听器
     */
    private ApplicationEventPublisher publisher;

    @Override
    public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
        this.publisher = applicationEventPublisher;
    }
    @Override
    public String update(RouteDefinition routeDefinition) {
        log.info("更新路由配置：{}",routeDefinition);
        this.routeDefinitionWriter.delete(Mono.just(routeDefinition.getId()));
        routeDefinitionWriter.save(Mono.just(routeDefinition)).subscribe();
        this.publisher.publishEvent(new RefreshRoutesEvent(this));
        return "success";
    }
    @Override
    public String add(RouteDefinition routeDefinition) {
        log.info("新增路由配置：{}",routeDefinition);
        routeDefinitionWriter.save(Mono.just(routeDefinition)).subscribe();
        //routeIds.add(routeDefinition.getId());
        this.publisher.publishEvent(new RefreshRoutesEvent(this));
        return "success";
    }
    //删除路由
    public String delete(String id){
        try{
            this.routeDefinitionWriter.delete(Mono.just(id));
            return "delete success";
        }catch (Exception e){
            e.printStackTrace();
            return "delete fail";
        }
    }

//    //路由ID
//    private static List<String> routeIds = Lists.newArrayList();
//
//    //监听nacos路由配置，动态改变路由
//    @NacosConfigListener(dataId = "gateway-routes")
//    public void routeConfigListener(String configInfo){
//        clearRoute();
//        List<RouteDefinition> gatewayRouteDefinition = JSON.parseArray(configInfo,RouteDefinition.class);
//        for (RouteDefinition routeDefinition:gatewayRouteDefinition){
//            add(routeDefinition);
//        }
//        publish();
//    }
//
//    @Override
//    public void update(RouteDefinition routeDefinition) {
//        log.info("更新路由配置：{}",routeDefinition);
//        this.routeDefinitionWriter.delete(Mono.just(routeDefinition.getId()));
//        routeDefinitionWriter.save(Mono.just(routeDefinition)).subscribe();
//        this.publisher.publishEvent(new RefreshRoutesEvent(this));
//    }
//
//    //清空路由
//    private void clearRoute(){
//        for (String id:routeIds){
//            routeDefinitionWriter.delete(Mono.just(id)).subscribe();
//        }
//        routeIds.clear();
//    }
//
//    @Override
//    public void add(RouteDefinition routeDefinition) {
//        log.info("新增路由配置：{}",routeDefinition);
//        routeDefinitionWriter.save(Mono.just(routeDefinition)).subscribe();
//        routeIds.add(routeDefinition.getId());
//        //this.publisher.publishEvent(new RefreshRoutesEvent(this));
//    }
//
//    @Override
//    public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
//        this.publisher = applicationEventPublisher;
//    }
//
//    //发布路由，使路由生效
//    private void publish(){
//        this.publisher.publishEvent(new RefreshRoutesEvent(this.routeDefinitionWriter));
//    }

}
